$(function () {
    FastClick.attach(document.body);
    fullWidth();
    $(window).resize(function () {
        fullWidth();
        // scrollStyle();
    });

    $('.d-footer-join').on('click',function () {
        var sign_type = $('#sign_type').val();
        if(sign_type == 0){
            $('.d-buy-ticket').show();
            hideBox($('.d-buy-ticket'));
        }else if(sign_type == 1){
            $('.d-tanchuang1').show();
            hideBox($('.d-tanchuang1'));
        }else if(sign_type == 2){
            $('.d-tanchuang2').show();
            hideBox($('.d-tanchuang2'));
        }
        $('.clickgray-4').show();
        $('.d-header').css('z-index','0');
        //此设置禁止移动端浏览器滚动 外层套个DIV
        $('#all').height($(window).height());
        $('#all').css('overflow','hidden');
    });

    //微信图片预览接口
    // $('#previewImage img:not(.ignore_wx)').on('click',function(event) {
    //     var imgArray = [];
    //     var curImageSrc = $(this).attr('src');
    //     var oParent = $(this).parent();
    //     if (curImageSrc && !oParent.attr('href')) {
    //         $('#previewImage img:not(.ignore_wx)').each(function(index, el) {
    //             var itemSrc = $(this).attr('src');
    //             imgArray.push(itemSrc);
    //         });
    //         wx.previewImage({
    //             current:curImageSrc,
    //             urls:imgArray
    //         });
    //     }
    // });

    //微信图片预览接口
    $('#previewImage img:not(.ignore_wx)').on('click',function(event) {
        var imgArray = [];
        var _httpReg = /^http.+$/gi;
        var curImageSrc = $(this).attr('src');
        if(!_httpReg.test(curImageSrc)){
            curImageSrc = "http://"+window.location.host.toString() + curImageSrc;
        }
        var oParent = $(this).parent();
        if (curImageSrc && !oParent.attr('href')) {
            $('#previewImage img:not(.ignore_wx)').each(function(index, el) {
                var itemSrc = $(this).attr('src');
                var _httpReg = /^http.+$/gi;
                if(!_httpReg.test(itemSrc)){
                    itemSrc = "http://"+window.location.host.toString() + itemSrc;
                }
                imgArray.push(itemSrc);
            });
            wx.previewImage({
                current:curImageSrc,
                urls:imgArray
            });
        }
    });

    $(".d-footer-msg").click(function(e){
        window.parent.openTip();
    });
    $(".d-att-btn").click(function(e){
        window.parent.openTip();
    });
    Preview.buyTicket();
    
    $(document).on('click','.closeIconBox',function () {
        closeBox();
    });

    var browser={
        versions:function(){
            var u = navigator.userAgent, app = navigator.appVersion;
            return {
                trident: u.indexOf('Trident') > -1, //IE内核
                presto: u.indexOf('Presto') > -1, //opera内核
                webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
                gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,//火狐内核
                mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
                android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
                iPhone: u.indexOf('iPhone') > -1 , //是否为iPhone或者QQHD浏览器
                iPad: u.indexOf('iPad') > -1, //是否iPad
                webApp: u.indexOf('Safari') == -1 //是否web应该程序，没有头部与底部
            };
        }(),
        language:(navigator.browserLanguage || navigator.language).toLowerCase()
}

    //用于处理edge ie11下的预览BUG
    if(browser.versions.mobile||browser.versions.android||browser.versions.ios){

    }else {
        // $('.d-att-btn img').css({'width':'15px','height':'15px','margin-top':'9px'});
    }
});

var Preview = {
    buyTicket:function () {
        $('.d-ticket-type').each(function () {
            var $this = $(this),
                $jia = $this.find('.d-ticket-jia'),
                $jian = $this.find('.d-ticket-jian'),
                $input = $this.find('input'),
                ticLimit = parseInt($this.find('.ticket-limit').attr('data-value')),
                ticCount = parseInt($this.find('.ticket-count').attr('data-value'));
            //type  1:+橙色  2：-橙色   11：+灰色  22：-灰色
            var btnColor = function (_type) {
                if(_type == 1){
                    $this.find('.d-ticket-jia img').attr('src','/Public/image/wx/jia-aa.png');
                }
                if(_type == 2){
                    $this.find('.d-ticket-jian img').attr('src','/Public/image/wx/jian-aa.png');
                }
                if(_type == 11){
                    $this.find('.d-ticket-jia img').attr('src','/Public/image/wx/jia-bb.png');
                }
                if(_type == 22){
                    $this.find('.d-ticket-jian img').attr('src','/Public/image/wx/jian-bb.png');
                }
            };
            $jia.on('click', function () {
                Preview.checkTicketNumInput($input, ticCount, ticLimit, 1);
                Preview.checkNextBtn();
                btnColor(2);
                Preview.btnStyle($input, ticCount, ticLimit,$this);
            });
            $jian.on('click', function () {
                Preview.checkTicketNumInput($input, ticCount, ticLimit, 2);
                Preview.checkNextBtn();
                btnColor(1);
                Preview.btnStyle($input, ticCount, ticLimit,$this);
            });
            $input.blur(function () {
                Preview.checkTicketNumInput(this, ticCount, ticLimit, 0);
                Preview.checkNextBtn();
                Preview.btnStyle($input, ticCount, ticLimit,$this);
            });
        });
    },
    checkTicketNumInput : function(_this, ticCount, ticLimit, type){ //$_this input 对象  //type 0：不做操作 1：++ 2：--
        var _ticNum = parseInt($(_this).val() ? $(_this).val() : 0);
        if(isNaN(_ticNum)){
            _ticNum = 0;
        }
        if(type == 1){
            _ticNum++;
        }else if(type == 2){
            _ticNum--;
        }
        if(_ticNum > 99999){
            _ticNum = 99999;
        }
        if(_ticNum < 0){
            _ticNum = 0;
        }
        if(_ticNum > ticCount && ticCount != -1){
            _ticNum = ticCount;
        }
        if(_ticNum > ticLimit && ticLimit != 0){
            if(ticLimit > ticCount && ticCount != -1){
                _ticNum = ticCount;
            }else{
                _ticNum = ticLimit;
            }
        }
        $(_this).val(_ticNum);
    },
    checkNextBtn : function(){
        var allNum = 0;
        $('.d-ticket-type').each(function () {
            allNum += +$(this).find('.ticket-num-input').val()
        });
        if(allNum == 0){
            $('.d-next-btn').css('background-color','#e7e7e7');
            $('.d-next-btn').attr('data-type', 0);
        }else{
            $('.d-next-btn').css('background-color','#f66926');
            $('.d-next-btn').attr('data-type', 1);
        }
    },
    showDetail:function () {
        $('#input_info').hide();
        $('#all').show();
        $('.p-count').children().remove();
    },
    showForm:function () {
        console.log('ssss');
        var _type = $('.d-next-btn').attr('data-type');
        if (_type == 1){
            var _data = [];
            var _total = [];
            $('.d-ticket-type').each(function () {
                var _title = $(this).find('.tic-title-span').text();
                var _price = $(this).find('.ticket-price-span').text();
                var _num = $(this).find('.ticket-num-input').val();

                console.log(_price);
                if(_num > 0){
                    _data.push({
                        title:_title,
                        price:_price,
                        num:_num
                    });
                }

                if(_price != '免费'){
                    _total.push({
                        total: parseFloat(_price) * parseInt(_num)
                    });
                }else if(_price == '免费'){
                    _total.push({
                        total: 0
                    });
                }
            });

            for (var i = 0; i < _data.length; i++){
                $('.p-count').append(
                    "<div class='p-t-box'>" +
                        "<span class='p-t-name'>" + _data[i].title + "</span>" +
                        "<span>" +
                            "<span style='font-size: 0.22rem'>x</span>" +
                            "<span>" + _data[i].num + "</span>" +
                        "</span>" +
                        "<span class='p-t-price'>" +
                            "<span class='p-price'>" + _data[i].price + "</span>" +
                        "</span>" +
                    "</div>"
                );
            }
            var Total = 0;
            for (var i = 0; i < _total.length; i++){
                Total += _total[i].total;
                console.log(Total);
                $('.ticket-total').text(Total);
            }
            $('#input_info').show();
            $('#all').hide();
        }else {
            layer.open({
                content: '请选择您要购买的票券'
                ,skin: 'msg'
                ,time: 2 //2秒后自动关闭
            });
        }
    },
    btnStyle:function (_input,ticLimit,ticCount,$this) {
        //type  1:+橙色  2：-橙色   11：+灰色  22：-灰色
        var btnColor = function (_type) {
            if(_type == 1){
                $this.find('.d-ticket-jia img').attr('src','/Public/image/wx/jia-aa.png');
            }
            if(_type == 2){
                $this.find('.d-ticket-jian img').attr('src','/Public/image/wx/jian-aa.png');
            }
            if(_type == 11){
                $this.find('.d-ticket-jia img').attr('src','/Public/image/wx/jia-bb.png');
            }
            if(_type == 22){
                $this.find('.d-ticket-jian img').attr('src','/Public/image/wx/jian-bb.png');
            }
        };
        var _val = parseInt($(_input).val());
        if(_val == ticLimit || _val == ticCount || _val == 99999){
            btnColor(11);
        }else {
            btnColor(1);
        }
        if(_val != 0){
            btnColor(2);
        }else {
            btnColor(22);
            btnColor(1);
        }
    }

};

// function scrollStyle() {
//     var bars = '.jspHorizontalBar, .jspVerticalBar';
//
//     $('.scroll-pane').bind('jsp-initialised', function (event, isScrollable) {
//
//         //hide the scroll bar on first load
//         $(this).find(bars).hide();
//
//     }).jScrollPane().hover(
//         //hide show scrollbar
//         function () {
//             $(this).find(bars).stop().fadeTo('fast', 0.9);
//         },
//         function () {
//             $(this).find(bars).stop().fadeTo('fast', 0);
//         }
//
//     );
// }

var activityId = getQueryString('id');


//弹出窗出来之后隐藏已购票提醒跟发布按钮
var hideBox = function (a) {
    var aDisplay = $(a).css('display');
    if (aDisplay  == 'block'){
        $('.d-have-tickets').hide();
        $('.d-release-at-btn').hide();
        $('.back-top-btn').hide()
    }else {
        $('.d-have-tickets').show();
        $('.d-release-at-btn').show();
        $('.back-top-btn').hide()
    }
};

//关闭后弹窗后需要的状态
var closeBox = function () {
        $('.d-release-at-btn').show();
        $('.d-write-msg,.clickgray-4').hide();
        $('.d-buy-ticket').hide();
        $('.d-header').css('z-index','1');
        $('body').unbind('touchmove');
        $('#all').css('overflow','auto');
        $('#all').css('height','auto');
};



